<?php

/**
 * Explanation actions.
 *
 * @package    5
 * @subpackage Explanation
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ExplanationActions extends sfActions
{
  public function executeShow(sfWebRequest $request)
  {
    $test = Doctrine_Core::getTable('Test')->findOneById($request->getParameter('test_id'));
    $this->forward404Unless($test);

    if ($test->getUserId() !== $this->getUser()->getId()) {
      $this->forward('sfGuardAuth', 'secure');
    }

    $this->explanation = Doctrine_Core::getTable('Explanation')->findOneByIdAndElementIdAndSubElementId($request->getParameter('id'), $test->getElementId(), $test->getSubElementId());
    $this->forward404Unless($this->explanation);

    if ((int)$this->explanation->getCost()) {
      $log = Doctrine_Core::getTable('paymentLog')->findOneByTestIdAndExplanationId($test->getId(), $this->explanation->getId());
      if (!$log) {
        $this->getResponse()->setStatusCode(402);
        $this->setTemplate('payment');
      }
    }
  }
}
